<?php

require MX_SHOP_PATH . 'core/web/mroles/page_merch.php';
class Index_MxShopPage extends MrolesWebPage {

    public function main(){

        global $_W;

        include $this->template('mroles/mendian/index');
    }

    public function detail(){

        global $_W,$_GPC;

        //创业者设备所在门店id
        $mendianid = array(0);
        if($_W['isowner']){
            $mendianid = $this->getOwnerGoods(1);
        }

        //店主门店id
        if($_W['ismanage']){
            $mdids = $this->getMendianGoods(1,1);
            $mendianid = array_merge($mendianid, $mdids);
            unset($mdids);
        }

        //服务商门店id
        if($_W['ischannel']){
            $chids = $this->getMendianGoods(2,1);
            $mendianid = array_merge($mendianid, $chids);
            unset($chids);
        }

        //运营商门店id
        if($_W['isoperator']){
            $opids = $this->getOperatorGoods(1);
            $mendianid = array_merge($mendianid, $opids);
            unset($opids);
        }

        $mendianid = implode(',', array_unique($mendianid));
        $mendianid = empty($mendianid) ? 0 : $mendianid;

        $condition = ' AND m.id in ('.$mendianid.') ';
        $pageparams = $params = array();

        if(!empty($_GPC['keyword'])){

            $type = $_GPC['type'];
            if($type == 'mendian'){
                $condition .= ' AND locate(:keyword,m.mendianname)>0 ';
            }else if($type == 'username') {
                $condition .= ' AND (locate(:keyword,b.realname)>0 or locate(:keyword,b.mobile)>0 ';
            }
            $params[':keyword'] = htmlspecialchars_decode($_GPC['keyword'], ENT_QUOTES);
            $pageparams['type'] = $type;
            $pageparams['keyword'] = $_GPC['keyword'];
        }

        $selprov = trim($_GPC['province']);
        if (!empty($selprov) && $selprov != '请选择省份') {
            $condition .= ' AND m.province=:province ';
            $params[':province'] = $selprov;
            $pageparams['province'] = $selprov;

            $selcity = trim($_GPC['city']);
            if (!empty($selcity) && $selcity != '请选择城市') {
                $condition .= ' AND m.city=:city ';
                $params[':city'] = $selcity;
                $pageparams['city'] = $selcity;

                $seldist = trim($_GPC['dist']);
                if (!empty($seldist) && $seldist != '请选择区域' && $seldist != '市辖区') {
                    $condition .= ' AND m.dist=:dist ';
                    $params[':dist'] = $seldist;
                    $pageparams['dist'] = $seldist;
                }
            }
        }

        $pindex = empty($_GPC['page']) ? 1 : intval($_GPC['page']);
        $psize = 10;

        $list = pdo_fetchall('SELECT m.id,m.mdcode,m.mendianname,m.province,m.city,m.status,b.realname,c.name as fenlei,i.name as hangye FROM '.tablename('mx_shop_mendian').' m '.
                                    'LEFT JOIN '.tablename('mx_shop_user').' b ON b.id=m.manageid '.
                                    'LEFT JOIN '.tablename('mx_shop_mendian_cate').' c ON c.id=m.mdcate_id '.
                                    'LEFT JOIN '.tablename('mx_shop_mendian_industry').' i ON i.id=m.industryid '.
                                    'WHERE m.deleted=0 '.$condition.
                                    'LIMIT '.($pindex-1) * $psize.','.$psize, $params);
        $total = pdo_fetchcolumn('SELECT count(*) FROM '.tablename('mx_shop_mendian').' m LEFT JOIN '.tablename('mx_shop_user').' b ON b.id=m.manageid WHERE m.deleted=0'.$condition, $params);
        $pager = owpager($total, $pindex, $psize, $pageparams);

        include $this->template('mroles/mendian/detail');
    }

    public function view(){

        global $_W,$_GPC;

        $id = intval($_GPC['id']);

        $mendian = pdo_fetch('SELECT o.*, m.realname, m.avatar, '.
            ' cm.realname as channel, cm.avatar as channelavatar, c.name as cate, i.name as industry '.
            'FROM '.tablename('mx_shop_mendian').' o '.
            'LEFT JOIN '.tablename('mx_shop_user').' m ON m.id=o.manageid '.
            'LEFT JOIN '.tablename('mx_shop_user').' cm ON cm.id=o.channelid '.
            'LEFT JOIN '.tablename('mx_shop_mendian_cate').' c ON c.id=o.mdcate_id '.
            'LEFT JOIN '.tablename('mx_shop_mendian_industry').' i ON i.id=o.industryid '.
            'WHERE o.id=:id',
            array(':id'=>$id)
        );

        if(empty($id) || empty($mendian)){
            $this->message('门店不存在!');
        }

        include $this->template('mroles/mendian/view');
    }

}